<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" @close="handleClose">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="名称" prop="title">
        <el-input v-model="dataForm.title" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="分类图标" prop="cover">
        <Imglist @updataImg="updataImg" :limit="1" :value="cover" :disable="true" :dialogVisible='visible'></Imglist>
      </el-form-item>
      <el-form-item label="是否显示" prop="open">
        <el-switch v-model="dataForm.open"> </el-switch>
      </el-form-item>
      <el-form-item label="是否置顶" prop="stick">
        <el-switch v-model="dataForm.stick"> </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import serviceApi from '@/api/serviceApi.js'
import Imglist from './../../oss/oss-uploadImg'
export default {
  components: {
    Imglist
  },
  data () {
    return {
      visible: false,
      is_pass: 1,
      dataForm: {
        title: '',
        cover: '',
        open: true,
        stick: true
      },
      cover: null,
      value: 1,

      dataRule: {
        title: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }]
        // cover: [{
        //   required: true,
        //   message: "图标不能为空",
        //   trigger: "blur"
        // }],
      }
    }
  },

  methods: {
    handleClose () {
      this.dataForm = {
        title: '',
        cover: null,
        open: true,
        stick: true,
        id: null
      }
    },
    updataImg (e) {
      console.log('e', e)
      this.dataForm.cover = e[0]
    },
    init (row) {
      this.visible = true

      // this.$nextTick(() => {

      //   this.$refs["dataForm"].resetFields();
      // });
      this.dataForm = {
        title: '',
        cover: null,
        open: true,
        stick: true,
        id: null
      }
      this.cover = []
      console.log('row', row)
      if (row) {
        this.dataForm = JSON.parse(JSON.stringify(row))
        this.cover = this.dataForm.cover ? this.dataForm.cover.split(',') : null
        console.log('this.cover', this.cover)
      }
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate(async (valid) => {
        if (valid) {
          let res
          if (this.dataForm.id) {
            if (!this.dataForm.cover) {
              this.dataForm.cover = ''
            }
            res = await serviceApi.themeUpdate(this.dataForm)
            if (res && res.data.code == 200) {
              this.$message.success('操作成功')
              this.visible = false
              this.$emit('getList')
            }
          } else {
            res = await serviceApi.themeAdd(this.dataForm)
            if (res && res.data.code == 200) {
              this.$emit('getList')
              this.$message.success('操作成功')
              this.visible = false
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
